package com.huidu.bitrade.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huidu.bitrade.dto.UOrderDTO;
import com.huidu.bitrade.entity.BaUorder;
import com.huidu.bitrade.request.util.BinanceUtil;
import com.huidu.bitrade.service.BaUorderService;
import com.huidu.bitrade.vo.UOrderVO;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@AllArgsConstructor
@Slf4j
public class BInanceUOrderController {
    @Autowired
    private BaUorderService service;

    @PostMapping("/newOrder")
    public UOrderDTO newOrder(UOrderVO orderVO) {
        UOrderDTO order = BinanceUtil.createOrder(orderVO);
        BaUorder uorder = new BaUorder();
        BeanUtils.copyProperties(order, uorder);
        service.save(uorder);
        return order;
    }

    @GetMapping("/getOrderById")
    public List<BaUorder> getOrderById(Long id) {
        List<BaUorder> orders = service.list(Wrappers.<BaUorder>lambdaQuery().eq(BaUorder::getMId, id));
        List<BaUorder> filteredOrders = orders.stream()
                .filter(order -> order.getClosePosition() == 1)
                .collect(Collectors.toList());
        return filteredOrders;
    }


}
